package com.example.aidldemo.server

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.os.RemoteCallbackList
import android.os.RemoteException
import android.util.Log
import com.example.aidldemo.aidl.Book
import com.example.aidldemo.aidl.IBookManager
import com.example.aidldemo.aidl.IOnNewBookAddListener
import java.util.concurrent.CopyOnWriteArrayList

/**
 * @author: njb
 * @date:   2025/3/22 17:34
 * @desc:   描述
 */
class BookManagerService :Service(){
    private val mBookList = CopyOnWriteArrayList<Book>()
    private val mListenerList = CopyOnWriteArrayList<IOnNewBookAddListener>()
    private val mRemoteCallbackList = RemoteCallbackList<IOnNewBookAddListener>()
    private val TAG: String = BookManagerService::class.java.name

    private val mBinder: Binder = object : IBookManager.Stub() {
        @Throws(RemoteException::class)
        override fun basicTypes(
            anInt: Int,
            aLong: Long,
            aBoolean: Boolean,
            aFloat: Float,
            aDouble: Double,
            aString: String?
        ) {
            Log.d(TAG,"--basicTypes-- $aString")
        }

        @Throws(RemoteException::class)
        override fun getBookList(): MutableList<Book> {
            return mBookList
        }

        @Throws(RemoteException::class)
        override fun addNewBook(book: Book?) {
           mBookList.add(book)
            onNewBookAdd(book)
        }

        @Throws(RemoteException::class)
        override fun registerListener(listener: IOnNewBookAddListener?) {
            if (!mListenerList.contains(listener)) {
                mListenerList.add(listener)
                Log.d(TAG, " -- registerListener -- this listener is add")
            } else {
                Log.d(TAG, " -- registerListener -- this listener already exist")
            }
        }

        @Throws(RemoteException::class)
        override fun unregisterListener(listener: IOnNewBookAddListener?) {
            if (mListenerList.contains(listener)) {
                mListenerList.remove(listener)
                Log.d(TAG, "-- unregisterListener -- listener is remove")
            } else {
                Log.d(TAG, "-- unregisterListener -- listener not found")
            }
        }

    }

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "-- onCreate -- ")
        mBookList.add(Book(1001, "Java入门到精通"))
        Thread(addRunnable()).start()
    }

    private inner class addRunnable : Runnable {
        override fun run() {
            try {
                Thread.sleep(3000)
            } catch (ie: InterruptedException) {
                ie.printStackTrace()
            }
            try {
                val book = Book(1002, "C++程序设计")
                onNewBookAdd(book)
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }
    }

    fun onNewBookAdd(book: Book?) {
        mBookList.add(book)
        for (j in mListenerList.indices) {
            val listener = mListenerList[j]
            listener.onNewBookAdd(book)
        }
    }

    override fun onBind(intent: Intent?): IBinder {
        return mBinder
    }

}